﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.Collections;

namespace Christmas_Card
{
    class Snow : EventObj
    {
        private const int noStar = 40;

        private Image starImage;

        private ArrayList starArray;

        private Timer clock;
        private const int interval = 15;

        Random rand;

        private Size frame;
        private Panel parent;

        public Snow(Size _frame, int _beginTime, Panel _parent) : base (_beginTime)
        {
            starImage = new Bitmap(global::Christmas_Card.Properties.Resources.Star);

            parent = _parent;
            frame = _frame;
            starArray = new ArrayList();

            rand = new Random();

            clock = new Timer();
            clock.Interval = interval;
            clock.Tick += new EventHandler(clock_Tick);

            initStar();
        }

        public void initStar()
        {
            for (int i = 0; i < noStar; i++)
            {
                Star star = new Star(starImage, frame, rand, parent);
                int xLocation = rand.Next(0, frame.Width - star.Width);
                int yLocation = rand.Next(0, frame.Height - star.Height);// -frame.Height;
                star.Location = new Point(xLocation, yLocation);
                starArray.Add(star);
                ((Scenario)parent).drawingObj.Add(star);
            }
        }

        void clock_Tick(object sender, EventArgs e)
        {
            foreach (Star star in starArray)
            {
                star.move();
                if (star.isOver())
                {
                    star.refresh();
                }
            }
        }

        public override void perform()
        {
            clock.Start();
        }
    }

    class Star : PictureBox
    {
        private const int minWidth = 10;
        private const int maxWidth = 20;
        private const int minHeight = 10;
        private const int maxHeight = 20;
        private const int minSpeed = 5;
        private const int maxSpeed = 10;

        private Image starImage;
        public int starWidth;
        public int starHeight;

        private int fallSpeed = 5;

        private Size frame;
        private Random rand;
        private Panel parentPanel;

        public Star(Image _starImage, Size _frame, Random _rand, Panel _parent) : base ()
        {
            starImage = _starImage;
            
            this.Image = starImage;
            this.SizeMode = PictureBoxSizeMode.StretchImage;

            this.frame = _frame;
            this.rand = _rand;

            refreshMeasure();
            

            //this.Parent = _parent;
            parentPanel = _parent;
        }

        public void move()
        {
            //if (((Scenario)parentPanel).drawingObj.Contains(this)) ((Scenario)parentPanel).drawingObj.Remove(this);
            this.Location = new Point(this.Location.X, this.Location.Y + fallSpeed);
            //((Scenario)parentPanel).drawingObj.Add(this);
            //UnDone
        }

        public bool isOver()
        {
            if (this.Location.Y > frame.Height) return true;
            return false;
        }

        public void refresh()
        {
            //Undone

            //Location
            refreshLocation();

            //Measure
            refreshMeasure();

            //Speed
            this.fallSpeed = rand.Next(minSpeed, maxSpeed);
        }

        private void refreshLocation()
        {
            int xLocation = rand.Next(0, frame.Width - starWidth);
            int yLocation = rand.Next(0, starHeight + 20) - 2 * starHeight - 20;
            this.Location = new Point(xLocation, yLocation);
        }

        private void refreshMeasure()
        {
            int widthM = rand.Next(minWidth, maxWidth);
            int heightM = rand.Next(minHeight, maxHeight);
            this.Size = new Size(widthM, heightM);
        }
    }
}
